“h5_crtgrpd18”

Information

The example shown in this file appears at: http://www.hdfgroup.org/HDF5/Tutor/crtgrpd.html (accessed 10 July 2011)

The example shown in this file is following the tutorial: http://www.hdfgroup.org/HDF5/Tutor/introductory.html

Good references for the shown HDF5 functions include:
http://www.hdfgroup.org/HDF5/doc/RM/RM_H5Front.html
http://www.hdfgroup.org/HDF5/doc/RM/

Prepared 10 July 2011 by Scot Martin for Windows 7, 64-bit, Mathematica version 8.0.1.

“c” code we seek to replicate in this example

/*
 *  Create two datasets within groups.
 */

#include "hdf5.h"
#define FILE "groups.h5"

int main() {

   hid_t       file_id, group_id, dataset_id, dataspace_id;  /* identifiers */
   hsize_t     dims[2];
   herr_t      status;
   int         i, j, dset1_data[3][3], dset2_data[2][10];

   /* Initialize the first dataset. */
   for (i = 0; i < 3; i++)
      for (j = 0; j < 3; j++)
         dset1_data[i][j] = j + 1;

   /* Initialize the second dataset. */
   for (i = 0; i < 2; i++)
      for (j = 0; j < 10; j++)
         dset2_data[i][j] = j + 1;

   /* Open an existing file. */
   file_id = H5Fopen(FILE, H5F_ACC_RDWR, H5P_DEFAULT);

   /* Create the data space for the first dataset. */
   dims[0] = 3;
   dims[1] = 3;
   dataspace_id = H5Screate_simple(2, dims, NULL);

   /* Create a dataset in group "MyGroup". */
   dataset_id = H5Dcreate(file_id, "/MyGroup/dset1", H5T_STD_I32BE, dataspace_id,
                          H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

   /* Write the first dataset. */
   status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
                     dset1_data);

   /* Close the data space for the first dataset. */
   status = H5Sclose(dataspace_id);

   /* Close the first dataset. */
   status = H5Dclose(dataset_id);

   /* Open an existing group of the specified file. */
   group_id = H5Gopen(file_id, "/MyGroup/Group_A", H5P_DEFAULT);

   /* Create the data space for the second dataset. */
   dims[0] = 2;
   dims[1] = 10;
   dataspace_id = H5Screate_simple(2, dims, NULL);

   /* Create the second dataset in group "Group_A". */
   dataset_id = H5Dcreate(group_id, "dset2", H5T_STD_I32BE, dataspace_id, 
                          H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);

   /* Write the second dataset. */
   status = H5Dwrite(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
                     dset2_data);

   /* Close the data space for the second dataset. */
   status = H5Sclose(dataspace_id);

   /* Close the second dataset */
   status = H5Dclose(dataset_id);

   /* Close the group. */
   status = H5Gclose(group_id);

   /* Close the file. */
   status = H5Fclose(file_id);
}

Implementation in Mathematica

SetDirectory[NotebookDirectory[]]

Needs["HDF5`"]

file=FileNameJoin[{Directory[],"groups.h5"}]

dataSet1Data=Table[j,{i,3},{j,3}]

dataSet2Data=Table[j,{i,2},{j,10}]

fileID=H5Fopen[file,H5FuACCuRDWR,H5PuDEFAULT]

dataSpaceID=H5ScreateUsimple[2,{3,3},Null]

dataSetID=H5Dcreate[fileID,"/MyGroup/dset1",H5T`copy[H5TuSTDuI32BE]@Id[],dataSpaceID,H5PuDEFAULT,H5PuDEFAULT,H5PuDEFAULT]

H5Dwrite[Integer32][dataSetID,H5T`copy[H5TuNATIVEuINT]@Id[],H5SuALL,H5SuALL,H5PuDEFAULT,Flatten@dataSet1Data]

H5Sclose[dataSpaceID]

H5Dclose[dataSetID]

groupID=H5Gopen[fileID, "/MyGroup/Group_A", H5PuDEFAULT]

dataSpaceID=H5ScreateUsimple[2,{2,10},Null]

dataSetID=H5Dcreate[groupID,"dset2",H5T`copy[H5TuSTDuI32BE]@Id[],dataSpaceID,H5PuDEFAULT,H5PuDEFAULT,H5PuDEFAULT]

H5Dwrite[Integer32][dataSetID,H5T`copy[H5TuNATIVEuINT]@Id[],H5SuALL,H5SuALL,H5PuDEFAULT,Flatten@dataSet2Data]

H5Sclose[dataSpaceID]
H5Dclose[dataSetID]
H5Gclose[groupID]
H5Fclose[fileID]

Implementation in Mathematica using the Extend Functionality

This example is similar to the above but written with a syntax that is closer to normal Mathematica style and uses the “HDF5Extend`” for this purpose.

Remember that this function assumes it is operating on a fresh copy of “groups.h5”. It cannot be run in direct sequence to the foregoing implementation since groups.h5 is no longer fresh, i.e., the H5Dcreate commands will fail.

SetDirectory[NotebookDirectory[]];
Needs["HDF5Extend`"];
file=FileNameJoin[{Directory[],"groups.h5"}];
dataSet1Data=Table[j,{i,3},{j,3}];
dataSet2Data=Table[j,{i,2},{j,10}]

H5FExtendOpen[
{fileID=H5Fopen[file,H5FuACCuRDWR,H5PuDEFAULT]},

CompoundExpression[

dataSpaceID=H5ScreateUsimple[2,{3,3},Null],

dataSetID=H5Dcreate[fileID,"/MyGroup/dset1",H5T`copy[H5TuSTDuI32BE]@Id[],dataSpaceID,H5PuDEFAULT,H5PuDEFAULT,H5PuDEFAULT],

H5DExtendWrite[dataSetID,H5T`copy[H5TuNATIVEuINT]@Id[],H5SuALL,H5SuALL,H5PuDEFAULT,dataSet1Data,"DataType"->Integer32],

H5Sclose[dataSpaceID],

H5Dclose[dataSetID],

H5GExtendOpen[
{groupID=H5Gopen[fileID, "/MyGroup/Group_A", H5PuDEFAULT]},

CompoundExpression[

dataSpaceID=H5ScreateUsimple[2,{2,10},Null],

dataSetID=H5Dcreate[groupID,"dset2",H5T`copy[H5TuSTDuI32BE]@Id[],dataSpaceID,H5PuDEFAULT,H5PuDEFAULT,H5PuDEFAULT],

H5DExtendWrite[dataSetID,H5T`copy[H5TuNATIVEuINT]@Id[],H5SuALL,H5SuALL,H5PuDEFAULT,dataSet2Data,"DataType"->Integer32],

H5Sclose[dataSpaceID],

H5Dclose[dataSetID]

] (* close CompoundExpression *)

] (* close H5GExtendOpen *)

] (* close CompoundExpression *)

] (* close H5FExtendOpen *)

Documentation of the employed Extend functions

??H5FExtendOpen

??H5GExtendOpen

??H5DExtendWrite